package command;

import parser.ChessContentHandlerConstants;
import parser.ParserConstants;

public class PiecePlaceCommand implements Command {

	private chess.Tile origTile;
	private String piece;
	private chess.Player player;
	private chess.Piece newPiece;
	private Command secondaryCommand;
	
	public PiecePlaceCommand(chess.Tile orig, String piece, chess.Player player){
		origTile = orig;
		this.piece = piece;
		this.player = player;
		secondaryCommand = new NoSecondaryCommand();
	}
	/** 
	 * This method will execute the command and place the piece on the tile for a player
	 */
	@Override
	public void execute() {
		newPiece = null;
		if(piece.equals("King")){
			newPiece = new chess.KingPiece(player,origTile);
		}else if(piece.equals("Knight")){
			newPiece = new chess.KnightPiece(player,origTile);
		}else if(piece.equals("Pawn")){
			newPiece = new chess.PawnPiece(player,origTile);
		}else if(piece.equals("Queen")){
			newPiece = new chess.QueenPiece(player,origTile);
		}else if(piece.equals("Rook")){
			newPiece = new chess.RookPiece(player,origTile);
		}else if(piece.equals("Bishop")){
			newPiece = new chess.BishopPiece(player,origTile);
		}
		origTile.setPieceHere(newPiece);
	}
	/**
	 * This method will return a boolean value depending if the piece has moved or not
	 * @return Returns true if the piece has move
	 */
	@Override
	public boolean hadMovedPreviously() {
		return false;
	}
	public chess.Tile getCurrentTile(){
		return origTile;
	}
	/**
	 * This method will set the secondary command
	 * @param secondaryCommand The secondary command to be set
	 */
	@Override
	public void addSecondaryCommand(Command secondaryCommand) {
		if (this.secondaryCommand instanceof NoSecondaryCommand){
			this.secondaryCommand = secondaryCommand;
		}
		else{
			this.secondaryCommand.addSecondaryCommand(secondaryCommand);
		}
	}
	/**
	 * This method when called will undo the command
	 */
	@Override
	public void undo() {
		origTile.setPieceHere(null);
		player.getMyPieces().remove(newPiece);
	}
	/**
	 * This method will return a string representation of the command
	 * @return Returns a string representation of the command
	 */
	public String toString(){
		return "Place: " + origTile.toString() + " to " + piece; 
	}
	/**
	 * This method will return a string representation of the command used for importing and
	 * exporting
	 * @return Returns a string representing the object
	 */
	@Override
	public String exportCode() {
		return "Place:" + origTile.toString() + "to" + piece; 
	}
	/**
	 * This method will return a string representation of the command used for saving and
	 * loading
	 * @return Returns a string representing the object
	 */
	@Override
	public String xmlCode() {
		String code = "<" + ParserConstants.ELEMENT_PLACE + ">\n";
		
		code += "t" + origTile.XMLPointTag(ChessContentHandlerConstants.POINT_1).replaceAll("\n", "\n\t");
		code += "\n";
		code += "t" + XMLPieceTag().replaceAll("\n", "\n\t");
		code += "\n";
		code += "</" + ParserConstants.ELEMENT_PLACE + ">";
		code += secondaryCommand.xmlCode();
		return code;
	}
	/**
	 * This method is a helper method to turn the piece object into an XML string
	 * @return Returns a string representing the piece
	 */
	private String XMLPieceTag(){
		String s = "<" + ChessContentHandlerConstants.ELEMENT_PIECE + " ";
		
		s += ChessContentHandlerConstants.ATTRIBUTE_VALUE + "='" + piece + "'>";
		
		s += "</" + ChessContentHandlerConstants.ELEMENT_PIECE + ">";
		return s;
	}
	@Override
	public Command getSecondaryCommand() {
		return secondaryCommand;
	}
}
